var common = require('../../utils/common.js');
const app = getApp()
Page({
  data: {
    classification: [],
    items: [],
    page: 1,
    loading: true,
    brandid: '',
    brandname: ''
  },
  onLoad: function (options) {
    console.log(options)
    this.setData({
      brandid: options.brandid,
      brandname: options.brandname
    })
    wx.setNavigationBarTitle({
      title: this.data.brandname
    })
    this.getItemData()
  },
  getItemData(){
    const that = this
    common.post({
      url: '/app/v2/goods/list', data: { brandId: this.data.brandid, sessionId: app.globalData.sessionId}
    })
      .then((res) => {
        console.log(res.data)
        that.setData({
          items: res.data.data,
        });
        if(res.data.total == 0){
          that.setData({
            noSpItem: true,
            loading: false
          })
        }else if(res.data.total < 11){
          that.setData({
            loading: false,
          });
        }
      }).catch((res) => {
        console.log(res)
      })
  },
  getItemDataPage() {
    const that = this;
    var page = that.data.page + 1;
    common.post({ url: '/app/v2/goods/list', data: { brandId: this.data.brandid, sessionId: app.globalData.sessionId, pageNo: page } })
      .then((res) => {
        var data = that.data.items;
        console.log(res.data)
        if (that.data.page * 10 < res.data.total) {
          that.setData({
            page: page
          });
          var postList = res.data.data;
          for (let i = 0; i < postList.length; i++) {
            let tmp = postList[i];
            data.push(tmp);
            setTimeout(function () {
              that.setData({
                items: data
              });
              console.log(data)
            }, 300)
          }
        } else {
          console.log('没有了')
          wx.showToast({
            title: '没有了',
            icon: 'none',
            duration: 2000
          })
          that.setData({
            loading: false,
          });
        }

      }).catch((res) => {
        console.log(res)

      })
  },
  onReachBottom() {
    console.log('bottom')
    this.getItemDataPage()
  },
  buybtn(e) {
    if (e.currentTarget.dataset.item.is99) {
      wx.navigateTo({
        url: '/pages/freeItemdetail/freeItemdetail?sku=' + e.currentTarget.dataset.item.skuId
      })
    }
  },
  closeTip(){
    this.setData({
      tipshow: false
    })
  },
  totop(){
    this.setData({
      scrollTop: 0
    })
  },
  seachlink: function () {
    wx.navigateTo({
      url: '/pages/seach/seach'
    })
  },

})